package org.yourscrum.domain;

import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author Jettro Coenradie
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Story {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk",value="true")
    private String encodedKey;

    @Persistent
    @Extension(vendorName="datanucleus", key="gae.pk-id", value="true")
    private Long id;

    @Persistent
    private String name;

    @Persistent
    private String description;

    @Persistent(mappedBy = "story")
    private List<Task> tasks;

    public Story(String name) {
        this.name = name;
    }

    public void changeName(String name) {
        this.name = name;
    }

    public void changeDescription(String description) {
        this.description = description;
    }

    public Task addNewTask(String description) {
        if (tasks == null) {
            tasks = new ArrayList<Task>();
        }
        Task newTask = new Task(this, description);
        this.tasks.add(newTask);
        return newTask;
    }

    public int getNumberOfTasks() {
        return tasks.size();
    }

    /* Getters */

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public List<Task> getTasks() {
        if (tasks == null) {
            tasks = new ArrayList<Task>();
        }
        return Collections.unmodifiableList(tasks);
    }
}
